package com.ruoyi.employment.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.employment.domain.EmploymentInfo;
import com.ruoyi.employment.mapper.EmploymentInfoMapper;
import com.ruoyi.employment.service.AddExcelService2;
import com.ruoyi.student.domain.StudentInfo;
import com.ruoyi.student.mapper.StudentInfoMapper;
import com.ruoyi.student.service.AddExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @projectName: employmentSystem
 * @package: com.ruoyi.student.service.impl
 * @className: AddExcelServiceImpl
 * @author: ximu
 * @description: TODO
 * @date: 2022/5/2 16:21
 * @version: 1.0
 */

@Service
public class AddExcelService2Impl implements AddExcelService2 {

    @Autowired
    private EmploymentInfoMapper employmentInfoMapper;

    @Override
    public String importData(List<EmploymentInfo> employmentInfoList, Boolean isUpdateSupport, String operName)
    {
        //判断导入数据是否为null
        if (StringUtils.isNull(employmentInfoList) || employmentInfoList.size() == 0)
        {
            return "导入数据不能为空";
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //查询已有数据
        List<EmploymentInfo> existList = employmentInfoMapper.selectEmploymentInfoList2();;
        for (EmploymentInfo importData : employmentInfoList)
        {

            try {

                boolean userFlag = false;
                //验证是否存在这个学生
                for (EmploymentInfo entry : existList) {
                    if (entry.getStudentId()==importData.getStudentId()) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    employmentInfoMapper.insertEmploymentInfo(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、名字为: " + importData.getName() + " 的就业信息导入成功");
                } else if (isUpdateSupport) {
                    employmentInfoMapper.updateEmploymentInfo(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、id为: " + importData.getStudentId() + " 的就业信息更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、id为: " + importData.getStudentId() + " 的就业信息已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、id为 " + importData.getStudentId() + "的就业信息导入失败：";
                failureMsg.append(msg + e.getMessage());

            }
        }
        if (failureNum > 0)
        {
            return "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确";
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



}
